<?php
/**
 * @author spenceryg
 * @date 2023/4/9
 * @time 6:43 下午
 * @focus love & life
 */


namespace app\api\controller;

use app\common\model\Orders;
use app\common\model\PointProducts;
use hg\apidoc\annotation as Apidoc;

/**
 * Class PointOrder
 * @package app\api\controller
 * @Apidoc\Title("充值管理")
 * @Apidoc\Group("api")
 */
class PointOrder extends Auth
{
    /**
     * list
     * @Apidoc\Title("订单列表")
     */
    public function list()
    {
        $list_rows = input('list_rows', 10, 'intval');
        $res = Orders::where('member_id', $this->request->uid)
            ->append(['item_name'])
            ->withAttr('item_name', function ($val, $data) {
                return PointProducts::where('id', $data['point_products_id'])->value('name');
            })
            ->order('id', 'desc')->paginate($list_rows);
        return success('',$res);
    }

    /**
     * hasNewOrder
     * @Apidoc\Title("是否有最新新订单")
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function hasNewOrder(): \think\response\Json
    {
        $order = Orders::where('member_id', $this->request->uid)
            ->where('create_time=update_time')
            ->where('create_time', '>', time() - 60 * 5)
            ->find();
        if($order){
            $order->force(true)->save();
        }
        $res = (bool)$order;
        return success('', $res);

    }
}
